<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function village_overview_list_page()
{
    @$output = drupal_render(drupal_get_form('village_overview_list_form'));
    return $output;
}

function village_overview_list_form($form, &$form_state)
{
    global $user;
    if (!empty($_GET['village_id'])) {
        $form_state['village'] = $_GET['village_id'];
    }
    if(!empty($form_state['village'])){
        $form['#village'] = $form_state['village'];
    }
    $form['#village_info'] = data_list_view_page($form, $form_state);

    return $form;
}
function data_list_view_page($form = '', &$form_state = '')
{
    global $user;
    drupal_add_js ( drupal_get_path ( 'module', 'village' ) . '/js/village.overview.js' );
    $user_info = _get_user_info($user->uid);
    //村落信息
    $village_info = nonghua_data_load('village', 'addr_id', $form_state['village']);
    if(!empty($village_info->distributor_id)){
        $query=db_select('distributor','D');
        $query->condition('D.distributor_id',$village_info->distributor_id,'=');
        $query->addField('D','distributor_name');
        $distributor_name = $query->execute()->fetchObject();
        $village_info->distributor_name = $distributor_name->distributor_name;
    }else{
        $village_info->distributor_name ="";
    }
    if(!empty($village_info->special_activities_id)){
        $special_activities = nonghua_data_load('village', 'addr_id', $form_state['village']);
        $village_info->special_activities_title = $special_activities->title;
    }else{
        $village_info->special_activities_title ="";
    }
    $address = nonghua_data_load("addr","addr_id",$form_state['village']);
    $village_info->name = $address->full_name;


    //村落活动信息
    $query = db_select('village_activity', 'va');
    $query->leftJoin('distributor', 'd', 'd.distributor_id=va.distributor_id');
    $query->leftJoin('special_activities', 's', 's.special_activities_id=va.special_activities_id');
    if (!empty( $form_state['village'])) {//
        $query->condition('va.addr_id', $form_state['village']);
    }
    if(!user_access('view all village_activity') && user_access('view own village_activity')){
        $query->condition('va.author_uid',$user->uid, '=');
    }
    $query->fields('va');
    $query->addField('d', 'distributor_name');
    $query->addField('s', 'title','sepeical_activities_title');
    $query->orderBy('va.timestamp', 'DESC');
    $query->range(0,20);
    $village_activity_info = $query->execute()->fetchAll();

    // 农户信息
    $query = db_select('farmer', 'f');
    $query->leftjoin('special_activities', 's', 's.special_activities_id=f.special_activities_id');
    $query->leftJoin('distributor', 'd', 'd.distributor_id=f.distributor_id');
    if (!empty( $form_state['village'])) {//
        $query->condition('f.addr_id', $form_state['village']);
    }
    if(!user_access('view all farmer') && user_access('view own farmer')){
        $query->condition('f.author_uid',$user->uid, '=');
    }
    $query->fields('f');
    $query->addField('s', 'title','sepeical_activities_title');
    $query->addField('d', 'distributor_name');
    $query->orderBy('f.timestamp', 'DESC');
    $query->range(0,20);
    $village_farmer_info = $query->execute()->fetchAll();

    // 种植信息
    $query = db_select('plant_info', 'p');
    $query->join('farmer', 'f', 'f.farmer_id=p.farmer_id');
    $query->leftJoin('village', 'v', 'v.addr_id=f.addr_id');
    $query->leftJoin('plant', 'pt', 'pt.plant_id=p.plant_id');
    $query->leftJoin('special_activities', 's', 's.special_activities_id=p.special_activities_id');
    if (!empty( $form_state['village'])) {//
        $query->condition('v.addr_id', $form_state['village']);
    }
    if(!user_access('view all plant_info') && user_access('view own plant_info')){
        $query->condition('p.author_uid',$user->uid, '=');
    }
    $query->fields('p');
    $query->addField('f', 'farmer_name');
    $query->addField('pt', 'plant_name');
    $query->addField('s', 'title','special_activities_title');
    $query->orderBy('p.timestamp', 'DESC');
    $query->range(0,20);
    $village_plant_info = $query->execute()->fetchAll();


    // 土壤信息
    $query = db_select('soil_info', 'si');
    $query->join('farmer', 'f', 'f.farmer_id=si.farmer_id');
    $query->leftJoin('village', 'v', 'v.addr_id=f.addr_id');
    $query->leftJoin('plant', 'pt1', 'pt1.plant_id=si.pre_plant_id');
    $query->leftJoin('plant', 'pt2', 'pt2.plant_id=si.rec_plant_id');
    $query->leftJoin('special_activities', 's', 's.special_activities_id=si.special_activities_id');
    if (!empty( $form_state['village'])) {//
        $query->condition('v.addr_id', $form_state['village']);
    }
    if(!user_access('view all soil_info') && user_access('view own soil_info')){
        $query->condition('si.author_uid',$user->uid, '=');
    }
    $query->fields('si');
    $query->addField('f', 'farmer_name');
    $query->addField('pt1', 'plant_name','pre_plant_name');
    $query->addField('pt2', 'plant_name','rec_plant_name');
    $query->addField('s', 'title','special_activities_title');
    $query->orderBy('si.timestamp', 'DESC');
    $query->range(0,20);
    $village_soil_info = $query->execute()->fetchAll();

    //施肥案例
    $query = db_select('fertilizer_case', 'C');
    $query->join('farmer', 'F', 'C.farmer_id=F.farmer_id');
    $query->join('village', 'V', 'V.addr_id=F.addr_id');
    $query->leftjoin('plant', 'P', 'C.plant_id=P.plant_id');
    $query->leftJoin('special_activities', 's', 's.special_activities_id=C.special_activities_id');
    if (!empty( $form_state['village'])) {//
        $query->condition('V.addr_id', $form_state['village']);
    }
    if(!user_access('view all fertilizer_case') && user_access('view own fertilizer_case')){
        $query->condition('C.author_uid',$user->uid, '=');
    }
    $query->fields('C');
    $query->addField('F', 'farmer_name');
    $query->addField('P', 'plant_name');
    $query->addField('s', 'title','special_activities_title');
    $query->orderBy('C.timestamp', 'DESC');
    $query->range(0,20);
    $village_fertilizer_case_info = $query->execute()->fetchAll();


    //回访记录
    $query = db_select('revisit', 'R');
    $query->join('farmer', 'F', 'F.farmer_id=R.farmer_id');
    $query->join('village', 'V', 'V.addr_id=F.addr_id');
    $query->leftJoin('plant', 'P', 'P.plant_id=R.plant_id');
    $query->leftJoin('special_activities', 's', 's.special_activities_id=R.special_activities_id');
    if (!empty( $form_state['village'])) {//
        $query->condition('V.addr_id', $form_state['village']);
    }
    if(!user_access('view all revisit') && user_access('view own revisit')){
        $query->condition('R.author_uid',$user->uid, '=');
    }
    $query->fields('R');
    $query->addField('F', 'farmer_name');
    $query->addField('P', 'plant_name');
    $query->addField('s', 'title','special_activities_title');
    $query->orderBy('R.timestamp', 'DESC');
    $query->range(0,20);
    $village_revisit_info = $query->execute()->fetchAll();

    //田间测产
    $query = db_select('plant_measure', 'PM');
    $query->join('farmer', 'F', 'PM.farmer_id=F.farmer_id');
    $query->join('village', 'V', 'V.addr_id=F.addr_id');
    $query->leftJoin('plant', 'P', 'PM.plant_id=P.plant_id');
    if (!empty( $form_state['village'])) {//
        $query->condition('V.addr_id', $form_state['village']);
    }
    if(!user_access('view all plant_measure') && user_access('view own plant_measure')){
        $query->condition('PM.author_uid',$user->uid, '=');
    }
    $query->fields('PM');
    $query->addField('F', 'farmer_name');
    $query->addField('P', 'plant_name');
    $query->orderBy('PM.timestamp', 'DESC');
    $query->range(0,20);
    $village_plant_measure_info = $query->execute()->fetchAll();


    $villageInfo =['village_info'=>$village_info,'village_activity_info'=>$village_activity_info,
        'village_plant_info'=>$village_plant_info,'village_farmer_info'=>$village_farmer_info,
        'village_soil_info'=>$village_soil_info,'village_fertilizer_case_info'=>$village_fertilizer_case_info,
        'village_revisit_info'=>$village_revisit_info,'village_plant_measure_info'=>$village_plant_measure_info];

    return $villageInfo;
}
